package xyj.sparrow.scheduler.conf;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import xyj.sparrow.scheduler.cron.ops.RedisMessageSync;
import xyj.sparrow.scheduler.cron.stream.CronConsumer;
import xyj.sparrow.scheduler.cron.stream.CronProvider;
import xyj.sparrow.scheduler.server.connect.ConnectManager;
import xyj.sparrow.scheduler.server.connect.DiscoveryConnectManager;

import javax.annotation.Resource;

/**
 * 分布式组件
 *
 * @author 江南小俊
 * @since 2021/6/21 下午11:19
 **/
@Configuration
@EnableConfigurationProperties({RedisConf.class, Model.class})
@ConditionalOnProperty(prefix = "xyj.sparrow", name = "mode", havingValue = "cluster")
public class ClusterAutoConfiguration {
    @Resource
    private RedisConf redisConf;

    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {

        return new LettuceConnectionFactory(new RedisStandaloneConfiguration(redisConf.getAddress(),
                redisConf.getPort()));
    }

    @Bean
    public StringRedisTemplate redisTemplate() {
        return new StringRedisTemplate(redisConnectionFactory());
    }

    @Bean
    public CronConsumer cronConsumer() {
        return new CronConsumer();
    }

    @Bean
    public CronProvider cronProvider() {
        return new CronProvider();
    }

    @Bean
    public RedisMessageSync redisMessageSync() {
        return new RedisMessageSync();
    }

    @Bean
    public ConnectManager discoveryConnectManager() {
        return new DiscoveryConnectManager();
    }
}
